package com.api.gbhg.luoao.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.ProjectDetailsUtil;
import com.api.gbhg.luoao.utils.Headquarters;
import com.api.gbhg.luoao.utils.ProcesscontentUtils;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("/infoprocess")
public class HeadcquartersApi {

    @POST
    @Path("/getHeadquarters")
    @Produces(MediaType.APPLICATION_JSON)
    public String getHeadquarters(Map<String,Object> param){
        PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
        //查询主表
        RecordSet primarytable = ProcesscontentUtils.getPrimarytable(param);
        primarytable.next();

        //流程基本信息
        Map<String,Object> Processbasic = new HashMap<>();
        Processbasic.put("requestId",primarytable.getStringNoTrim("requestId"));//流程id
        Processbasic.put("bt", ProjectDetailsUtil.getFlowIdByRequestName(primarytable.getStringNoTrim("requestId")));//标题

        String cjr = primarytable.getStringNoTrim("cjr");
        if (StrUtil.isNotEmpty(cjr)){
            //创建人
            RecordSet rs = rmcd.getUserNameById(primarytable.getInt("cjr"));
            rs.next();
            cjr =  rs.getStringNoTrim("loginid");
        }

        Processbasic.put("cjr",cjr);//创建人

        Processbasic.put("cjrq",primarytable.getStringNoTrim("cjrq"));//创建日期

        String cjrks = primarytable.getStringNoTrim("cjrks");
        if (StrUtil.isNotEmpty(cjrks)){
            // 创建人科室
            RecordSet cj = rmcd.getSBKSById(primarytable.getInt("cjrks"));
            cj.next();
            cjrks = cj.getStringNoTrim("departmentmark");
        }
        Processbasic.put("cjrks",cjrks);//创建人科室


        String jbr = primarytable.getStringNoTrim("jbr");
        if (StrUtil.isNotEmpty(jbr)){
            //经办人
            RecordSet jb = rmcd.getUserNameById(primarytable.getInt("jbr"));
            jb.next();
            jbr =  jb.getStringNoTrim("loginid");
        }
        Processbasic.put("jbr",jbr);//经办人

        String bbdw = primarytable.getStringNoTrim("bbdw");
        if (StrUtil.isNotEmpty(bbdw)){
            // 编报单位
            RecordSet  bb= rmcd.getSBDWById(primarytable.getInt("bbdw"));
            bb.next();
            bbdw = bb.getStringNoTrim("SUBCOMPANYNAME");
        }
        Processbasic.put("bbdw",bbdw);//编报单位


        String sydw = primarytable.getStringNoTrim("sydw");
        if (StrUtil.isNotEmpty(bbdw)){
            // 使用单位
            RecordSet  sy= rmcd.getSBDWById(primarytable.getInt("sydw"));
            sy.next();
            sydw = sy.getStringNoTrim("SUBCOMPANYNAME");
        }
        Processbasic.put("sydw",sydw);//使用单位

        Processbasic.put("yxysswy",primarytable.getStringNoTrim("yxysswy"));//一下预算数(万元)




        //项目基本信息
        Map<String,Object> basic = new HashMap<>();
        basic.put("sheet1_main_field1",primarytable.getStringNoTrim("sheet1_main_field1"));//所属一级项目
        basic.put("sheet1_main_field3",primarytable.getStringNoTrim("sheet1_main_field3"));//项目单位
        basic.put("sheet1_main_field5",primarytable.getStringNoTrim("sheet1_main_field5"));//项目名称
        basic.put("sheet1_main_field2",primarytable.getStringNoTrim("sheet1_main_field2"));//科目代码
        basic.put("sheet1_main_field4",primarytable.getStringNoTrim("sheet1_main_field4"));//项目代码


        //项目属性信息
        Map<String,Object> property = new HashMap<>();

        //项目类别
        String xmlb = ProcesscontentUtils.getAttributes("sheet1_main_field6",
                primarytable.getStringNoTrim("sheet1_main_field6"));

        property.put("sheet1_main_field6",xmlb);//项目类别

        property.put("sheet1_main_field8", primarytable.getStringNoTrim("sheet1_main_field8"));//计划开始执行年份
        property.put("sheet1_main_field10", primarytable.getStringNoTrim("sheet1_main_field10"));//最新批复年份

        //保密期限单位
        String bmqxdw = ProcesscontentUtils.getAttributes("sheet1_main_field12",
                primarytable.getStringNoTrim("sheet1_main_field12"));
        property.put("sheet1_main_field12", bmqxdw);//保密期限单位

        //是否科研项目
        String sfkyxm = ProcesscontentUtils.getAttributes("sheet1_main_field14",
                primarytable.getStringNoTrim("sheet1_main_field14"));
        property.put("sheet1_main_field14",sfkyxm);//是否科研项目

        //基建属性
        String jjsx = ProcesscontentUtils.getAttributes("sheet1_main_field67",
                primarytable.getStringNoTrim("sheet1_main_field67"));
        property.put("sheet1_main_field67", jjsx);//基建属性

        //项目周期
        String xmzq = ProcesscontentUtils.getAttributes("sheet1_main_field9",
                primarytable.getStringNoTrim("sheet1_main_field9"));
        property.put("sheet1_main_field9", xmzq);//项目周期

        //密级
        String mj =ProcesscontentUtils.getAttributes("sheet1_main_field11",
                primarytable.getStringNoTrim("sheet1_main_field11"));
        property.put("sheet1_main_field11", mj);//密级


        property.put("sheet1_main_field13", primarytable.getStringNoTrim("sheet1_main_field13"));//保密期限


        //项目管理信息
        Map<String,Object> management = new HashMap<>();
        management.put("sheet1_main_field15",primarytable.getStringNoTrim("sheet1_main_field15"));//项目负责人
        management.put("sheet1_main_field17",primarytable.getStringNoTrim("sheet1_main_field17"));//联系人电话
        management.put("sheet1_main_field16",primarytable.getStringNoTrim("sheet1_main_field16"));//职务



        //项目其他信息
        Map<String,Object> other = new HashMap<>();
        //是否横向标识
        String sfhxbs = ProcesscontentUtils.getAttributes("sheet1_main_field18",
                primarytable.getStringNoTrim("sheet1_main_field18"));
        other.put("sheet1_main_field18",sfhxbs);//是否横向标识

        other.put("sheet1_main_field19", primarytable.getStringNoTrim("sheet1_main_field19"));//财政审核状态

        //是否标识上报财政库
        String sfbssbczk = ProcesscontentUtils.getAttributes("sheet1_main_field20",
                primarytable.getStringNoTrim("sheet1_main_field20"));
        other.put("sheet1_main_field20", sfbssbczk);//是否标识上报财政库


        //统计口径设置
        Map<String,Object> statistics = new HashMap<>();
        statistics.put("sheet1_main_field21", primarytable.getStringNoTrim("sheet1_main_field21"));//打捆标识

        //项目基本信息
        Map<String,Object>[] list = new HashMap[5];
        list[0] = new HashMap<>();
        list[0].put("basic",basic);

        list[1] = new HashMap<>();
        list[1].put("property",property);

        list[2] = new HashMap<>();
        list[2].put("management",management);

        list[3] = new HashMap<>();
        list[3].put("other",other);

        list[4] = new HashMap<>();
        list[4].put("statistics",statistics);



        //项目支出立项依据
        Map<String,Object> prove = new HashMap<>();
        prove.put("sheet2_main_field1",primarytable.getStringNoTrim("sheet2_main_field1"));//项目名称
        prove.put("sheet2_main_field2",primarytable.getStringNoTrim("sheet2_main_field2"));//项目单位
        prove.put("sheet2_main_field3",primarytable.getStringNoTrim("sheet2_main_field3"));//主管单位及代码
        prove.put("sheet2_main_field5",primarytable.getStringNoTrim("sheet2_main_field5"));//项目开始年份

        //项目类别
        String yjlb = ProcesscontentUtils.getAttributes("sheet2_main_field4",
                primarytable.getStringNoTrim("sheet2_main_field4"));
        prove.put("sheet2_main_field4",yjlb);//项目类别

        //项目周期
        String yjzq = ProcesscontentUtils.getAttributes("sheet2_main_field6",
                primarytable.getStringNoTrim("sheet2_main_field6"));
        prove.put("sheet2_main_field6",yjzq);//项目周期
        prove.put("sheet2_main_field7",primarytable.getStringNoTrim("sheet2_main_field7"));//项目支出立项依据


        //项目支出实施方案
        Map<String,Object> scheme = new HashMap<>();
        scheme.put("sheet3_main_field1", primarytable.getStringNoTrim("sheet3_main_field1"));//项目名称
        scheme.put("sheet3_main_field2", primarytable.getStringNoTrim("sheet3_main_field2"));//项目单位
        scheme.put("sheet3_main_field3", primarytable.getStringNoTrim("sheet3_main_field3"));//主管单位及代码
        scheme.put("sheet3_main_field5", primarytable.getStringNoTrim("sheet3_main_field5"));//项目开始年份

        //项目类别
        String fayjzq = ProcesscontentUtils.getAttributes("sheet3_main_field4",
                primarytable.getStringNoTrim("sheet3_main_field4"));
        scheme.put("sheet3_main_field4", fayjzq);//项目类别
        //项目周期
        String faxmzq = ProcesscontentUtils.getAttributes("sheet3_main_field6",
                primarytable.getStringNoTrim("sheet3_main_field6"));
        scheme.put("sheet3_main_field6", faxmzq);//项目周期


        scheme.put("sheet3_main_field7",primarytable.getStringNoTrim("sheet3_main_field7"));//项目支出实施方案


        String mainid = primarytable.getStringNoTrim("id");

        //细化经济分类
        RecordSet Detail2 = ProcesscontentUtils.getDetail2(mainid);
        Map<String,Object>[] economic = new HashMap[Detail2.getArray().size()];
        int d = 0;
        int p = 1;
        while (Detail2.next()){
            economic[d] = new HashMap<>();
            economic[d].put("xh",p++);
            economic[d].put("sheet4_detail2_field1",Detail2.getStringNoTrim("sheet4_detail2_field1"));//年度

            String bm = Detail2.getStringNoTrim("sheet4_detail2_field2");
            if (StrUtil.isNotEmpty(bm)){
                bm = ProcesscontentUtils.getsubjectname(bm);
            }
            economic[d].put("sheet4_detail2_field2", bm);//部门预算支出经济分类科目
            economic[d].put("sheet4_detail2_field3", Detail2.getStringNoTrim("sheet4_detail2_field3"));//小计
            economic[d].put("sheet4_detail2_field4", Detail2.getStringNoTrim("sheet4_detail2_field4"));//年初数
            economic[d].put("sheet4_detail2_field5",  Detail2.getStringNoTrim("sheet4_detail2_field5"));//已调整数
            economic[d].put("sheet4_detail2_field6",  Detail2.getStringNoTrim("sheet4_detail2_field6"));//结转资金

            String zf = Detail2.getStringNoTrim("sheet4_detail2_field7");
            if (StrUtil.isNotEmpty(zf)){
                zf = ProcesscontentUtils.getsubjectname(zf);
            }
            economic[d].put("sheet4_detail2_field7", zf);//对应的政府预算支出经济分类科目
            d++;
        }


        //查询明细表3
        RecordSet Detail3 = ProcesscontentUtils.getDetail3(mainid);
        Map<String,Object>[] disburse = new HashMap[Detail3.getArray().size()];
        int i = 0;
        int k = 1;
        while (Detail3.next()){
            disburse[i] = new HashMap<>();
            disburse[i].put("xh",k++);
            disburse[i].put("sheet5_detail_field1",Detail3.getStringNoTrim("sheet5_detail_field1"));//项目活动
            disburse[i].put("sheet5_detail_field2", Detail3.getStringNoTrim("sheet5_detail_field2"));//对项目活动的描述
            disburse[i].put("sheet5_detail_field3", Detail3.getStringNoTrim("sheet5_detail_field3"));//子活动
            disburse[i].put("sheet5_detail_field4", Detail3.getStringNoTrim("sheet5_detail_field4"));//对子活动的描述
            disburse[i].put("sheet5_detail_field5",  Detail3.getStringNoTrim("sheet5_detail_field5"));//分项支出
            disburse[i].put("sheet5_detail_field6",  Detail3.getStringNoTrim("sheet5_detail_field6"));//数量/频率
            disburse[i].put("sheet5_detail_field7", Detail3.getStringNoTrim("sheet5_detail_field7"));//价格/标准
            disburse[i].put("sheet5_detail_field8",Detail3.getStringNoTrim("sheet5_detail_field8"));//支出计划（万元）
            disburse[i].put("sheet5_detail_field9", Detail3.getStringNoTrim("sheet5_detail_field9"));//备注
            i++;
        }

        Headquarters hqs = new Headquarters();

        hqs.setProcessbasic(Processbasic);//流程基本信息
        hqs.setList(list);//项目基本信息
        hqs.setProve(prove);//项目支出立项依据
        hqs.setScheme(scheme);//项目支出实施方案
        hqs.setEconomic(economic); //细化经济分类
        hqs.setDisburse(disburse);//支出明细


        return JSON.toJSONString(hqs);
    }
}
